<script>
export default {
  created () {
    var _this=this;
     //获取当前用户的token值 用于判断用户是否登陆过这个小程序
     var userToken=wx.getStorageSync('userToken');
    wx.login({
      success: res => {
        //发送res.code到后台换取openId , sessionKey , unionId
        var code= res.code;
        _this.$store.dispatch('setCode',code);
        wx.getSetting({
          success(res){
            if (res.authSetting['scope.userInfo']){
              //已经授权
              wx.getUserInfo({
                success(res){
                    _this.$store.dispatch('setuserInfo',res.userInfo)
                    if(!userToken)
                    {
                        var rawData = res.rawData;
                        var signature = res.signature;
                        var encryptedData = res.encryptedData;
                        var iv = res.iv;
                        wx.request({
                            url:'https://daka.rzkeji.com/api/auth/wxLogin',
                            data: {
                            "code":  _this.$store.state.code,
                            "raw_data": rawData,
                            "signature": signature,
                            'iv': iv,
                            'encryptedData': encryptedData
                            },
                            method: "POST",
                            header: {
                            'content-type': 'application/json'
                            },
                            success(res){
                                if(res.data.level=="success")
                                {
                                    wx.setStorageSync('userToken', res.data.data);
                                }else {
                          wx.showToast({
                            title: res.data.message,
                            icon: 'none',
                            duration: 2000
                          })
                        }
                            }
                        })
                    }
                }
              })
            }else{
              //没有授权的, 跳转到授权页面
                wx.navigateTo({
                  url:'../login/main'
                })
            }
          }
        })
      }
    })
  }
}
</script>

<style>
page{width: 100%;height: 100%;}
.clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}
.clearfloat{zoom:1}
</style>
